<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:c="http://java.sun.com/jstl/core"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:nxl="http://nuxeo.org/nxforms/layout"
  xmlns:nxu="http://nuxeo.org/nxweb/util">

  <div class="bubbleBox">
    <h3 class="bubbleHeader">
      <h:outputText value="#{messages['label.security.parentDocumentsRights']}"/>
    </h3>

    <h:form>
      <h:dataTable var="user" class="dataTableLine" rowClasses="dataRowEven,dataRowOdd"
        columnClasses="${columnHeader.id=='c4' ? 'iconColumn' : ''}"
        value="#{securityActions.parentDocumentsUsers}"
        rendered="#{securityActions.displayInheritedPermissions}">

        <nxu:methodResult name="principalType"
          value="#{principalListManager.getPrincipalType(user)}">

          <h:column>
            <f:facet name="header">
              <h:outputText value="#{messages['label.content.header.type']}"/>
            </f:facet>

            <h:graphicImage value="#{securityActions.iconPathMap[principalType]}"
              title="#{securityActions.iconAltMap[principalType]}"/>

          </h:column>

          <h:column>
            <f:facet name="header">
              <h:outputText value="#{messages['label.username']}"/>
            </f:facet>

            <h:outputLink value="#{nxu:userUrl('user', user, null, false)}"
              rendered="#{principalType=='USER_TYPE'}">
              <h:outputText value="#{nxu:userFullName(user)}"/>
            </h:outputLink>
            <h:commandLink value="#{nxu:groupFullName(user)}" immediate="true"
              action="#{groupManagementActions.viewGroup(user)}"
              rendered="#{principalType!='USER_TYPE'}">
            </h:commandLink>

          </h:column>
        </nxu:methodResult>
        <h:column>
          <f:facet name="header">
            <h:outputText value="#{messages['label.security.grantedPerms']}"/>
          </f:facet>
          <ui:repeat var="perm" varStatus="status"
            value="#{securityActions.securityData.parentDocsGrantLabels[user]}">
            <h:outputText value="#{messages[perm]}"/>#{(status.last)?'':', '}
          </ui:repeat>
        </h:column>

        <h:column>
          <f:facet name="header">
            <h:outputText value="#{messages['label.security.deniedPerms']}"/>
          </f:facet>
          <ui:repeat var="perm" varStatus="status"
            value="#{securityActions.securityData.parentDocsDenyLabels[user]}">
            <h:outputText value="#{messages[perm]}"/>#{(status.last)?'':', '}
          </ui:repeat>
        </h:column>
      </h:dataTable>
    </h:form>

    <p>
      <h:form id="block_inherit">
        <h:selectBooleanCheckbox class="checkBox" value="#{securityActions.blockRightInheritance}"
          immediate="true" onclick="getElementById('block_inherit:submitBlock').click();"/>
        <h:outputText value="#{messages['label.security.block.inehritance']}"/>
        <h:commandButton id="submitBlock" value="Block inheritance"
          action="#{securityActions.blockRightInheritance()}"
          styleClass="visibilityH"/>
      </h:form>
    </p>

    <p class="emptyResult">
      <h:outputText rendered="${empty securityActions.parentDocumentsUsers}"
        value="#{messages['label.security.noParentDocumentsRights']}"/>
    </p>
  </div>

  <div class="bubbleBox">
    <h3 class="bubbleHeader">
      <h:outputText value="#{messages['label.security.localRights']} "/>
      <h:graphicImage value="/icons/warning.gif" title="#{messages['label.security.saveNeeded']}"
        rendered="#{securityActions.securityData.needSave}"/>
    </h3>

    <a4j:outputPanel id="panel_local_rights">
      <h:form id="rights_content">
        <h:panelGroup rendered="#{not empty securityActions.currentDocumentUsers}">
          <table class="dataTableLine" id="dataTable_local_rights">
            <thead>
            <tr>
              <th class="iconColumn">
                <h:selectBooleanCheckbox
                  value="#{securityActions.dataTableModel.selected}">
                  <f:ajax execute="@this"
                    render="security_button_remove panel_local_rights"/>
                </h:selectBooleanCheckbox>
              </th>
              <th>
                <h:outputText value="#{messages['label.content.header.type']}"/>
              </th>
              <th>
                <h:outputText value="#{messages['label.username']}"/>
              </th>
              <th>
                <h:outputText value="#{messages['label.security.grantedPerms']}"/>
              </th>
              <th>
                <h:outputText value="#{messages['label.security.deniedPerms']}"/>
              </th>
            </tr>
            </thead>
            <tbody>
            <c:forEach var="item" items="#{securityActions.dataTableModel.entries}">
              <tr>
                <td>
                  <h:selectBooleanCheckbox
                    value="#{item.selected}">
                    <f:ajax event="click" execute="@this"
                      render="security_button_remove"/>
                  </h:selectBooleanCheckbox>
                </td>
                <nxu:set var="user" value="#{item.data}">
                  <nxu:set var="principalType" value="#{principalListManager.getPrincipalType(user)}">
                    <td>
                      <h:graphicImage value="#{securityActions.iconPathMap[principalType]}"
                        title="#{securityActions.iconAltMap[principalType]}"/>
                    </td>
                    <td>
                      <h:outputLink value="#{nxu:userUrl('user', user, null, false)}"
                        rendered="#{principalType=='USER_TYPE'}">
                        <h:outputText value="#{nxu:userFullName(user)}"/>
                      </h:outputLink>
                      <h:commandLink value="#{nxu:groupFullName(user)}" immediate="true"
                        action="#{groupManagementActions.viewGroup(user)}"
                        rendered="#{principalType!='USER_TYPE'}">
                      </h:commandLink>
                    </td>
                  </nxu:set>

                  <td>
                    <ui:repeat var="perm" varStatus="status"
                      value="#{securityActions.securityData.currentDocGrantLabels[user]}">
                      <h:outputText value="#{messages[perm]}"/>#{(status.last)?'':', '}
                    </ui:repeat>
                  </td>
                  <td>
                    <ui:repeat var="perm" varStatus="status"
                      value="#{securityActions.securityData.currentDocDenyLabels[user]}">
                      <h:outputText value="#{messages[perm]}"/>#{(status.last)?'':', '}
                    </ui:repeat>
                  </td>
                </nxu:set>
              </tr>
            </c:forEach>
            </tbody>
          </table>
        </h:panelGroup>

        <p class="emptyResult"><h:outputText
          rendered="${empty securityActions.currentDocumentUsers}"
          value="#{messages['label.security.noLocalRights']}"/>
        </p>
        <h:commandButton
          id="security_button_remove"
          value="#{messages['command.security.delPerm']}" class="button floatL"
          rendered="${not empty securityActions.currentDocumentUsers}"
          action="#{securityActions.removePermissions}"
          disabled="#{!securityActions.canRemoveSecurityRules}"/>
        <h:panelGroup id="security_button_remove_script">
          <h:outputScript target="#{pageJsTarget}">
            var checked = isOneCheckBoxChecked("dataTable_local_rights");
            var btn = document.getElementById("rights_content:security_button_remove");
            if (btn) {
              if (checked) {
                btn.removeAttribute("disabled");
              }
              else {
                btn.setAttribute("disabled", "disabled");
              }
            }
          </h:outputScript>
        </h:panelGroup>
      </h:form>
    </a4j:outputPanel>

    <h:form id="validate_rights">
      <h:commandButton
        value="#{messages['label.security.save']}"
        class="button"
        action="#{securityActions.saveSecurityUpdates}"
        rendered="#{securityActions.securityData.needSave}"
        id="document_rights_validate_button"/>
    </h:form>

    <div class="clear"></div>
  </div>

  <div class="bubbleBox">
    <h3 class="bubbleHeader">
      <h:outputText value="#{messages['label.security.addNewRule']}"/>
    </h3>

    <a4j:region renderRegionOnly="true">
      <h:form id="add_rights_form">
        <h:outputScript target="#{pageJsTarget}">
          jQuery(document).ready(function () {
            jQuery('#add_rights_form').focusFirst();
          });
        </h:outputScript>
        <table class="dataInput">
          <tbody>
          <tr>
            <td>
              <nxl:layout name="user_group_suggestion" mode="edit"
                value="#{securityActions.selectedEntries}"/>
              <ui:include src="/incl/document_rights_action.xhtml"/>
            </td>
          </tr>
          </tbody>
        </table>
      </h:form>
    </a4j:region>
  </div>

</ui:composition>
